package org.bedracket.pgeg.datagen;

import net.minecraft.core.HolderLookup;
import net.minecraft.core.RegistrySetBuilder;
import net.minecraft.core.registries.Registries;
import net.minecraft.data.DataGenerator;
import net.minecraft.data.PackOutput;
import net.minecraftforge.common.data.DatapackBuiltinEntriesProvider;
import net.minecraftforge.common.data.ExistingFileHelper;
import net.minecraftforge.data.event.GatherDataEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.bedracket.pgeg.PGEGMod;
import org.bedracket.pgeg.datagen.lang.ModLanguageProvider;
import org.bedracket.pgeg.datagen.lang.ModLanguageProviderZh;
import org.bedracket.pgeg.datagen.levelgen.ModConfiguredFeatureProvider;
import org.bedracket.pgeg.datagen.levelgen.ModPlacedFeatureProvider;
import org.bedracket.pgeg.datagen.loot.ModLootTableProvider;

import java.util.Set;
import java.util.concurrent.CompletableFuture;

/**
 * PGEG Mod Datagen Class
 * Auto generate json file
 * @see net.minecraftforge.data.event.GatherDataEvent
 */
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD)
public class ModDataGenerator {

    public static RegistrySetBuilder BUILDER =
            new RegistrySetBuilder()
                    .add(Registries.PLACED_FEATURE,
                            ModPlacedFeatureProvider::placedFeature)
                    .add(Registries.CONFIGURED_FEATURE,
                            ModConfiguredFeatureProvider::configuredFeatures);

    @SubscribeEvent
    public static void onGatherData(GatherDataEvent event) {
        DataGenerator generator = event.getGenerator();
        PackOutput output = generator.getPackOutput();
        ExistingFileHelper helper = event.getExistingFileHelper();
        boolean isClient = event.includeClient();
        boolean isServer = event.includeServer();
        CompletableFuture<HolderLookup.Provider> lookup = event.getLookupProvider();
        generator.addProvider(isClient, new ModItemModelProvider(output, helper));
        generator.addProvider(isServer, new ModRecipeProvider(output));
        generator.addProvider(isClient, new ModLanguageProvider(output));
        generator.addProvider(isClient, new ModLanguageProviderZh(output));
        generator.addProvider(isServer, new ModBlockTagsProvider(output, lookup, helper));
        generator.addProvider(isServer, new ModItemTagsProvider(output, lookup,
                new ModBlockTagsProvider(output, lookup, helper), helper));
        generator.addProvider(isClient, new ModBlockStateProvider(output, helper));
        generator.addProvider(isServer, new ModLootTableProvider(output));
        generator.addProvider(isServer,
                new DatapackBuiltinEntriesProvider(output,
                        lookup, BUILDER,
                        Set.of(PGEGMod.MOD_ID)));
    }
}
